﻿using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System.Windows;
using ReflectionStudio.Core.FTPClient;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    using System.Data.OracleClient;
    using System.IO;
    public class JLTVK05APPReport : BaseReport
    {
  
        string sheetName = "";
 
        public bool IsOnAuditing = false;
        public bool IsSendForAuditing = false;
        Dictionary<string, int> qpplan = new Dictionary<string, int>(); 
        public bool IsApproved = false;
        public FTPClient ftpClient;
        SETINTEXDB INTEXDB = new SETINTEXDB();
        INTEXDBC INTEXDBC;
        public JLTVK05APPReport()
            : base()
        {
        }
        public JLTVK05APPReport(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
            UserID = (string)dicParamater["User"];
            if (dicParamater.ContainsKey("ROOTDRIVE"))
            {
                Root = dicParamater["ROOTDRIVE"].ToString();
            }
            else
            {
                Root = "C:\\IntexReport";
            }
            if (dicParamater.ContainsKey("NETDRIVE"))
            {
                Root = dicParamater["NETDRIVE"].ToString();
            }
            else
            {
                Root = "R:";
            }

            INTEXDBC = new INTEXDBC(OracleDB, proxy);
           
        }
        public override void WriteExcelReport(string fileType)
        {
            try
            { 
                strExcelFile = @"JLT\JLTStandard.xls";
                Standard = "VKPOSPR";
                INTEXDBC.OracleDB = OracleDB;
                INTEXDBC.proxy = proxy;
                #region=================检查路径======================
                destfolder = SetFolder(Root, "已审批");
                destfolder = SetFolder(Root, "备份");
                destfolder = SetFolder(Root, "临时");
                #endregion
                DataRow dwData = INTEXDB.SDDAT.NewRow();
                dwData["SDSA"] = "0013";
                dsSource.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
                dwData["SDSA"] = "0114";
                dsSource.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));

                dwData["SDSA"] = "0734";
                dsSource.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
                dwData["SDSA"] = "0477";
                dsSource.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
                dsSource.Merge(INTEXDBC.QueryHRSTAMM(dwData,3));

                #region==========准备程序===================

                TemplatePath = Path.Combine(PathHelper.ApplicationPath, @"Report\" + strExcelFile);
                DataRow dwVKKOPF = dsSource.Tables["VKKOPF"].Rows[0];
                string year = dwVKKOPF["VKKNEUDAT"].ToString().Substring(0, 4);
                string month = dwVKKOPF["VKKNEUDAT"].ToString().Substring(4, 2);
                string day = dwVKKOPF["VKKNEUDAT"].ToString().Substring(6, 2);
   
                string KRS = dsSource.Tables["VKPOS"].Rows[0]["VKPAUFKRS"].ToString().Trim();
                string LFD = dsSource.Tables["VKPOS"].Rows[0]["VKPAUFLFD"].ToString().Trim(); 
                long flfd = 0;
                flfd = System.Convert.ToInt64(LFD);
                string ordernumer = KRS.Trim() + flfd.ToString();

                exAgent.CheckWorkBook(ordernumer);
                #endregion
                ServerShareFile(KRS, year, month, ordernumer, fileType); 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void SetTXTValue(string sgb,  int x, int y)
        {
            int length = 0;
            string s1 = "";
            string s2 = "";
            try
            {
                s1 = sgb;
                exAgent.SetCellValue(x, y, s1);
                x++;
                return;
                length = sgb.Length;
                if (length <= 50)
                {
                    s1 = sgb;
                    exAgent.SetCellValue(x, y, s1);
                    x++;
                    return;
                }
                s1 = sgb.Substring(0, 50);
                exAgent.SetCellValue(x, y, s1);
                x++;
                s2 = sgb.Substring(50, length - 50);
                exAgent.SetCellValue(x, y, s2);
                x++;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void FillInExcelData(ArrayList columns, int xStart = 1, int yStart = 0)
        {
            string msg = "";
            string sdkey = "";
            try
            {
                if (!dsSource.Tables.Contains("VAART"))
                {
                    return;
                }
                if (!dsSource.Tables.Contains("VAFARBEN"))
                {
                    return;
                }
                string src = "";
                string sgb = ""; 
                DataTable tbVKKOPF = dsSource.Tables["VKKOPF"];
                DataTable tbVKPOS = dsSource.Tables["VKPOS"];
                DataRow dwVKP = tbVKPOS.Rows[0];
                string datalink = "";
                string KRS;
                string LFD;
                string POS;
                string str1 = "",str2="";
                string TXTISIND; 
                int position = 0; 
                DataRow dwVKKOPF = tbVKKOPF.Rows[0];
                DataTable tbVKPBEST = new DataTable(), tbVKK07 = new DataTable();
                DataTable TTGAAUF = new DataTable();
                string VKPMEEH = "";
                string printdate, orderdate = ""; 
                int positions = 0; 
                DataTable tbVA07 = dsSource.Tables["VAART"];
                DataTable tbVA10 = dsSource.Tables["VAFARBEN"];
                DataRow dwVA07 = tbVA07.Rows[0];
                DataRow dwVA10 = null;
                double VKKQTY = 0;
                string meeh ="MTR";
                string text = "";
                string user = "";
                #region=============VKKOPF==================
                
                src = "任务书号：" + dwVKKOPF["VKKAUFKRS"].ToString() + "-" + System.Convert.ToInt64(dwVKKOPF["VKKAUFLFD"].ToString());
                exAgent.SetCellValue(3, 1, src);
                src = dwVKKOPF["VKKBESTNR"].ToString();// "合同号:" +
                exAgent.SetCellValue(3, 16, src);

                src = dwVKP["VKPPOSTYP"].ToString();//"记号:" 
                exAgent.SetCellValue(3, 35, src);

                string exfactroy = "";
                string VKKKUNDNR = dwVKKOPF["VKKKUNDNR"].ToString();
                //src = System.Convert.ToInt64(VKKKUNDNR).ToString()+"#";//"客户编号：" +
                //exAgent.SetCellValue(4, 5, src);
                exAgent.SetCellValue(4, 5, GetGB2312Value(dwVKP["KDKNAME"].ToString()));
                src = dwVKKOPF["VKKNEUBEN"].ToString();//跟单员:" +
                src = Helpers.Helper.GetHRName(dsSource, src);
                exAgent.SetCellValue(8, 35, src);
                user = src;
                string year = dwVKKOPF["VKKNEUDAT"].ToString().Substring(0, 4);
                string month = dwVKKOPF["VKKNEUDAT"].ToString().Substring(4, 2);
                string day = dwVKKOPF["VKKNEUDAT"].ToString().Substring(6, 2);
                orderdate = "下单日期：" + year + "/" + month + "/" + day;
          
                exAgent.SetCellValue(1,34, orderdate);

                year = DateTime.Now.Year.ToString();
                month = DateTime.Now.Month.ToString();
                day = DateTime.Now.Day.ToString();
            
                printdate = "打印日期：" + year + "/" + month + "/" + day;
                exAgent.SetCellValue(2, 34, printdate);

                src =Helpers.Helper.GetCompositionVA(dsSource,dwVA07);
                exAgent.SetCellValue(6, 16, src);
                string vbrt = dwVA07["VAAMEBRT"].ToString().Trim();

                src = dwVA07["VAABRTBIS"].ToString();
                double v2 = System.Math.Round(System.Convert.ToDouble(src) * 0.01, 0);
                exAgent.SetCellValue(5, 35, (v2 - 1) + "-" + v2 + vbrt);

                src = dwVA07["VAABRTVON"].ToString();                 
                 double v1 = System.Math.Round(System.Convert.ToDouble(src) * 0.01,0);
                 exAgent.SetCellValue(6, 35, v1 + vbrt); 
                 
                src = dwVA07["VAAPRGR"].ToString();                
                sdkey = "00" + Left(src + "                                           ", 21) + "V";
                src = INTEXDBC.GetSDDAT("0196", sdkey);
                exAgent.SetCellValue(6, 5, GetGB2312Value(src.Substring(1, 20)).Trim());
                src = dwVA07["VAAARTGRP"].ToString();
                sdkey = "00" + Left(src + "                                           ", 21) + "V";
                src = INTEXDBC.GetSDDAT("0178", sdkey);
                exAgent.SetCellValue(4, 16, GetGB2312Value(src.Substring(1, 20)).Trim());                	

                src = dwVA07["VAAFILLER"].ToString().Substring(90, 1);
                src = Helper.GetWeave(dsSource, src);
                exAgent.SetCellValue(7, 35, src);
                #region============组织规格VAD===================
                tbVKK07 = new DataTable();
                string koll = dwVKP["VKPKOLL"].ToString();
                string art = dwVKP["VKPART"].ToString();
                text = "";
                TXTISIND = koll + art.Trim();
                var qvad = from dwTemp in dsSource.Tables["TXTKON"].AsEnumerable()
                           where dwTemp["TXTIPIND"].ToString().Trim() == TXTISIND
                           select dwTemp;
                if (qvad.Count() > 0)
                {
                    tbVKK07 = qvad.CopyToDataTable();
                }
                foreach (DataRow dwTXTKON in tbVKK07.Rows)
                {
                    for (int ti = 1; ti < 40; ti++)
                    {
                        string ci = "TXT" + Right("00" + ti, 2) + "0";
                        src = dwTXTKON[ci].ToString(); src = GetGB2312Value(src).Trim();
                        if (src == "")
                            break;
                        text += src;
                    }
                }
                exAgent.SetCellValue(5, 16, text);
                #endregion
                exAgent.SetCellValue(8, 5, dwVKP["VKPKOLL"].ToString());
                meeh = dwVKP["VKPMEEH"].ToString();
                exAgent.SetCellValue(80, 17, "数量" + Environment.NewLine + meeh);
                #endregion
             #region============成件要求RECH===================

                src = dwVKP["VKPAUFMART"].ToString();
                string mu = Helper.GetMukeup(dsSource, src);
                exAgent.SetCellValue(11, 5, GetGB2312Value(mu));
                    #endregion
             
                #region============外观质量LIEF===================
                tbVKK07 = new DataTable();
                KRS = dwVKKOPF["VKKAUFKRS"].ToString();
                LFD = dwVKKOPF["VKKAUFLFD"].ToString();
                TXTISIND = KRS + LFD + "000" + "LIEF";
                var query2 = from dwTemp in dsSource.Tables["TXTKON"].AsEnumerable()
                             where dwTemp["TXTIPIND"].ToString().Contains(TXTISIND)
                             select dwTemp;
                if (query2.Count() > 0)
                {
                    tbVKK07 = query2.CopyToDataTable();
                }
                foreach (DataRow dwTXTKON in tbVKK07.Rows)
                {
                    for (int ti = 1; ti < 40; ti++)
                    {
                        string ci = "TXT" + Right("00" + ti, 2) + "0";
                        src = dwTXTKON[ci].ToString(); src = GetGB2312Value(src).Trim();
                        if (src == "")
                            break;
                        Helper.SplitString(ref src, ref str1, ref str2);
                        exAgent.SetCellValue(11+ ti, 1, str1);
                        exAgent.SetCellValue(11 + ti, 5, str2);                        
                    }
                }
                #endregion

                #region ===================内在质量=====================
                #region=================QPZUORD========================
                DataRow dwQPZUORD = null;
                if (dsSource.Tables.Contains("QPZUORD"))
                {
                    foreach (DataRow dwTemp in dsSource.Tables["QPZUORD"].Rows)
                    {
                        dwQPZUORD = dwTemp;
                    }
                    if (dwQPZUORD == null)
                    {
                        msg = "请检查质检计划信息：" + KRS + LFD;
                        EventDispatcher.Instance.RaiseStatus(msg, StatusEventType.StopProgress);
                    }
               
                 FillInQPPLAN(dwQPZUORD,42);
                }
                #endregion
                //交期
                #endregion
                #region============计品部意见BEST===================
                tbVKK07 = new DataTable();
                KRS = dwVKKOPF["VKKAUFKRS"].ToString();
                LFD = dwVKKOPF["VKKAUFLFD"].ToString();
                TXTISIND = KRS + LFD + "000" + "BEST";
                var query5 = from dwTemp in dsSource.Tables["TXTKON"].AsEnumerable()
                             where dwTemp["TXTIPIND"].ToString().Contains(TXTISIND)
                             select dwTemp;
                if (query5.Count() > 0)
                {
                    tbVKK07 = query5.CopyToDataTable();
                }
               text = "";
                foreach (DataRow dwTXTKON in tbVKK07.Rows)
                {
                    for (int ti = 1; ti < 40; ti++)
                    {
                        string ci = "TXT" + Right("00" + ti, 2) + "0";
                        src = dwTXTKON[ci].ToString(); sgb = GetGB2312Value(src).Trim();
                        if (sgb.Trim() == "")
                            continue;
                        exAgent.SetCellValue(31 - 1 + ti, 5, sgb);          
                    }
                }
                #endregion
                #region============其他要求IVDI===================
                tbVKK07 = new DataTable(); 
               KRS = dwVKKOPF["VKKAUFKRS"].ToString();
                LFD = dwVKKOPF["VKKAUFLFD"].ToString();
                TXTISIND = KRS + LFD + "000" + "IVDI";
                var qvor = from dwTemp in dsSource.Tables["TXTKON"].AsEnumerable()
                        where dwTemp["TXTIPIND"].ToString().Contains(TXTISIND)
                        select dwTemp;
                if (qvor.Count() > 0)
                {
                    tbVKK07 = qvor.CopyToDataTable();
                }
  
               text = "";
                foreach (DataRow dwTXTKON in tbVKK07.Rows)
                {
                    for (int ti = 1; ti < 40; ti++)
                    {
                        string ci = "TXT" + Right("00" + ti, 2) + "0";
                        src = dwTXTKON[ci].ToString(); sgb = GetGB2312Value(src).Trim();
                        if (sgb.Trim() == "")
                            continue;
                        exAgent.SetCellValue(70 - 1 + ti, 5, sgb);
                    }
                }
                #endregion 
                #region============花型要求IVVT===================

                DataTable tbSTD = new DataTable();
                KRS = dwVKKOPF["VKKAUFKRS"].ToString();
                LFD = dwVKKOPF["VKKAUFLFD"].ToString();
                TXTISIND = KRS + LFD + "000" + "IVVT";
                var query7 = from dwTemp in dsSource.Tables["TXTKON"].AsEnumerable()
                             where dwTemp["TXTIPIND"].ToString().Contains(TXTISIND)
                             select dwTemp;
                if (query7.Count() > 0)
                {
                    tbSTD = query7.CopyToDataTable();
                }  
                #endregion 

                xStart = 81;
                int x = xStart;
                string finish = "";
                int count = tbVKPOS.Rows.Count;
                positions = 0;
                DataRow dwKDART = null;
                foreach (DataRow dwVKPOS in tbVKPOS.Rows)
                {
                    if (dwVKPOS["VKPKKOLL"].ToString().Trim() != "")
                        exfactroy = dwVKPOS["VKPKKOLL"].ToString().Trim();
                    dwKDART = null;
                    positions++;
                    msg = "开始写入：" + position.ToString() + "行";
                    EventDispatcher.Instance.RaiseStatus(msg, StatusEventType.StopProgress);
                    KRS = dwVKPOS["VKPAUFKRS"].ToString();
                    LFD = dwVKPOS["VKPAUFLFD"].ToString();
                    POS = dwVKPOS["VKPAUFPOS"].ToString();
                    #region====================VKPOS=======================
                    POS = dwVKPOS["VKPAUFPOS"].ToString();
                    exAgent.SetCellValue(x, 1, System.Convert.ToInt64(POS));
                    src = dwVKPOS["VKPART"].ToString().TrimEnd();
                    sgb = GetGB2312Value(src);
                    exAgent.SetCellValue(x, 3, sgb);
                    #endregion
                    #region================组织===================
                    dwVA07 = null;
                    var q07 = from DataRow row in tbVA07.AsEnumerable()
                              where row["VAAART"].ToString() == dwVKPOS["VKPART"].ToString()
                              select row;
                    foreach (DataRow row in q07)
                    {
                        dwVA07 = row;
                    }
                    dwVA10 = null;
                    var q10 = from DataRow row in tbVA10.AsEnumerable()
                              where row["VAFART"].ToString() == dwVKPOS["VKPART"].ToString()
                              && row["VAFFARBE"].ToString() == dwVKPOS["VKPFARBE"].ToString()
                              select row;
                    foreach (DataRow row in q10)
                    {
                        dwVA10 = row;
                    }
                    src = dwVA10["VAFBESCHICH"].ToString().Trim();
                    sdkey = "00" + Left(src + "                                           ", 21);
                    src = INTEXDBC.GetSDDAT("0275", sdkey);
                    src=GetGB2312Value(src.Substring(1, 48)).Trim();
                    int idx = 0;
                    idx = src.IndexOfAny(new char[] { '(', '（' });
                    if (idx > 0)
                        src = src.Substring(0, idx);
                    if(!finish.Contains(src+";"))
                        finish+=src+";";
                    exAgent.SetCellValue(7, 5,(finish+"***").Replace(";***",""));
                    exAgent.SetCellValue(x, 11, src);
                    exAgent.SetCellValue(x, 8, dwVKPOS["VKPFARBE"].ToString().TrimEnd() + Environment.NewLine + GetGB2312Value(dwVA10["VAFBEZ"].ToString().Trim()));
                    #endregion 
                    #region================客人花号/品名======================
                    src = dwVKPOS["VKPKDARTNR"].ToString().TrimEnd() + " " + dwVKPOS["VKPKDBEZ"].ToString().TrimEnd() + " " + dwVKPOS["VKPKDFARBNR"].ToString().TrimEnd();
                    var qkd = from DataRow row in dsSource.Tables["VAKDBEZ"].AsEnumerable()
                              where row["VAKBKOLL"].ToString() == dwVKPOS["VKPKOLL"].ToString()
                              && row["VAKBART"].ToString() == dwVKPOS["VKPART"].ToString()
                              && row["VAKBFARBE"].ToString() == dwVKPOS["VKPFARBE"].ToString()
                              select row;
                    foreach (DataRow rkd in qkd)
                    {
                        src = rkd["VAKBKDARTNR"].ToString().TrimEnd() + " " + rkd["VAKBKDARTBEZ"].ToString().TrimEnd() + " " + rkd["VAKBKDFARBNR"].ToString().TrimEnd();
                        dwKDART = rkd;
                    }
                    sgb = GetGB2312Value(src);
                    exAgent.SetCellValue(x + 1, 3, sgb);
                    #endregion
                    #region================数量===================
                    src = dwVKPOS["VKPMENGE1"].ToString();
                    string VKPMENGE1 = Divide(src, 100);
                    VKKQTY += System.Convert.ToDouble(VKPMENGE1);
                    exAgent.SetCellValue(x, 17, VKPMENGE1);
                    if (VKPMEEH.Trim() == "YRD")
                    {
                        VKPMENGE1 = (System.Math.Round(System.Convert.ToDouble(VKPMENGE1) * 0.9144, 0)).ToString();
                    }
                    #endregion

                    #region =================交期======================
                    src=dwVKPOS["VKPLIEFTERM"].ToString();
                    str1 = "";
                    str2 = "";
                    string str3 = "";
                    Helper.GetSplitDate(ref  str1,ref str2,ref str3,src);
                    exAgent.SetCellValue(x, 20,str3); 
                    //船样 
                    #endregion 
                    #region =================标准====================
                    text = "";
                    string std = GetGB2312Value(dwVKPOS["VKPZUSATZ"].ToString().Trim());
                    foreach (DataRow dwTXTKON in tbSTD.Rows)
                    {
                        for (int ti = 1; ti < 40; ti++)
                        {
                            string ci = "TXT" + Right("00" + ti, 2) + "0";
                            src = dwTXTKON[ci].ToString(); sgb = GetGB2312Value(src).Trim();
                            text = sgb.TrimEnd();
                            if (text.Trim() == "")
                                break;
                            int idxd = text.IndexOf(".");
                            if (idxd > 0)
                            {
                                string line = text.Substring(0, idxd);
                                line = Right("000" + line, 3);
                                if (line == POS)
                                    std += text.Substring(idxd + 1);
                            }
                        }
                    }
                    exAgent.SetCellValue(x, 25, std);
                    #endregion
                    if (positions < count)
                    {
                        exAgent.RangeCopy(x, x + 1, x + 2, x + 3);
                    }
                    
                    x = x + 2;
                }
                if (meeh.Trim() == "YRD")
                {
                    VKKQTY = System.Math.Round(System.Convert.ToDouble(VKKQTY) * 0.9144, 0);
                }
                //exAgent.SetCellValue(4, 35, VKKQTY+meeh);
                exAgent.SetCellValue(5, 5, exfactroy);
                exAgent.SetCellValue(4, 35, VKKQTY + "米");
                FillFooter(x + 1,user);
                src =dwVKKOPF["VKKSACHBE"].ToString();
                src = Helper.GetHRName(dsSource, src);
                exAgent.SetCellValue(x+1, 6, src);
            }
            catch (Exception ex)
            {
                msg = "有错误产生，请注意:";
                EventDispatcher.Instance.RaiseStatus(msg, StatusEventType.StopProgress);
                Tracer.Error(ex.StackTrace, ex);
                MessageBox.Show(msg + ex.Message);
            }
            finally
            {
                msg = "指令表虽然已经生产，但请注意核对。";
                EventDispatcher.Instance.RaiseStatus(msg, StatusEventType.StopProgress);
            }
        }
        public void FillFooter(int x,string user)
        {
            exAgent.SetCellValue(x, 1, "营销人员:");
            exAgent.SetCellValue(x, 11, "工厂负责人:");
            exAgent.SetCellValue(x, 21, "制单:");
            exAgent.SetCellValue(x, 24, user);
            exAgent.SetCellValue(x, 31, "审核:");
        }
        private void FillInQPPLAN(DataRow dwQPZUORD, int xstart)
        {
            try
            {
                int x = xstart;
                int y = 5;
                if (dwQPZUORD == null)
                    return;
                string QPZPNRKRS = "";
                string QPZPLFDNR = "";
                y = 5;
                if (dwQPZUORD != null)
                {
                    QPZPNRKRS = dwQPZUORD["QPZPNRKRS"].ToString().Trim();
                    QPZPLFDNR = dwQPZUORD["QPZPLFDNR"].ToString().Trim();
                    exAgent.SetCellValue(x, 1, QPZPNRKRS + "-" + System.Convert.ToInt64(QPZPLFDNR));
                }
                DataTable QPPLAN = dsSource.Tables["QPPLAN"];
                DataTable QPSTEP = dsSource.Tables["ICC_QPSTEP"];
                DataRow dwQPSTEP = null;
                string processed = "";
                string src, sgb;

                int row = 0;
                int ix = 0;
                bool UseFixed = true;
                string ppos = "";
                int lg = 0;
                int cl = 1;
                int rw = 1;
                //QPSASWERT
                //QPSNSWERT
                if (QPSTEP.Rows.Count > 0)
                {
                    dwQPSTEP = QPSTEP.Rows[0];
                    ppos = dwQPSTEP["QPSFEHLANT"].ToString();
                    lg = ppos.Trim().Length;
                    if (lg > 2)
                    {
                        UseFixed = false;
                    }
                }
                string source = "";
                foreach (DataRow dwTemp in QPSTEP.Rows)
                {
                    source = dwTemp["QPSNAUS"].ToString();
                    dwQPSTEP = dwTemp;
                    ppos = dwQPSTEP["QPSFEHLANT"].ToString();
                    lg = ppos.Trim().Length;
                    cl = 1;
                    rw = 1;
                    if (lg > 2)
                    {
                        rw = System.Convert.ToInt32(ppos.Substring(0, lg - 2));
                        cl = System.Convert.ToInt32(ppos.Substring(lg - 2, 2));
                    }
                    string QPSDRUCKKZ = dwQPSTEP["QPSDRUCKKZ"].ToString().Trim();
                    if (QPSDRUCKKZ != "B")
                        continue;
                    string QPSSTEP = dwQPSTEP["QPSSTEP"].ToString() + ";";
                    if (processed.Contains(QPSSTEP))
                    {
                        continue;
                    }
                    src = dwQPSTEP["QPMKURZB"].ToString();//名称
                    sgb = GetGB2312Value(src.Substring(0, 15));
                    x = xstart + row;
                    y = 5 + ix * 19;
                    if (rw > 0)
                    {
                        x = xstart - 1 + rw;
                        y = 5 + (cl / 10 - 1) * 19;
                    }
                    exAgent.SetCellValue(x, y, sgb);

                    src = dwQPSTEP["QPSNSWERT"].ToString().TrimEnd();//范围
                    double QPSNSWERT = System.Convert.ToDouble(Divide(src, 1000));

                    src = dwQPSTEP["QPSNSUG"].ToString().TrimEnd();
                    sgb = GetGB2312Value(src);
                    double QPSNSUG = System.Convert.ToDouble(Divide(src, 1000));

                    src = dwQPSTEP["QPSNSOG"].ToString().TrimEnd();
                    sgb = GetGB2312Value(src);
                    double QPSNSOG = System.Convert.ToDouble(Divide(src, 1000));
                    //                    QPSUGKZ	QPSOGKZ

                    double min = QPSNSWERT + QPSNSUG;
                    double max = QPSNSWERT + QPSNSOG;
                    string smin = dwQPSTEP["QPSUGKZ"].ToString().Trim();
                    string smax = dwQPSTEP["QPSOGKZ"].ToString().Trim();
                    string scope = min + "~" + max;
                    if (smax == "O")
                    {
                        scope = "≥" + min;
                    }
                    if (smin == "O")
                    {
                        scope = "≤" + smax;
                    }// ≤ 
                    if (QPSNSWERT == 0)
                    {
                        scope = GetGB2312Value(dwQPSTEP["QPSASWERT"].ToString().Trim());
                    }
                    if (dwQPSTEP["QPSNAUS"].ToString().TrimEnd() == "WS08")
                    {
                        min = System.Convert.ToDouble(Divide(dwQPSTEP["WAPMIN"].ToString(), 1000));
                        max = System.Convert.ToDouble(Divide(dwQPSTEP["WAPMAX"].ToString(), 1000)); ;
                        scope = min + "~" + max;
                        if (min == 0)
                        {
                            scope = "≤" +max;
                        }
                        if (max == 0)
                        {
                            scope = "≥" + min;
                        }
                        /*if (smax == "O")
                        {
                            scope = "≥" + min;
                        }
                        if (smin == "O")
                        {
                            scope = "≤" + smax;
                        }// ≤ 
                         */
                    } 
                    x = xstart + row;
                    y = 5 + ix * 19;
                    if (rw > 0)
                    {
                        x = xstart - 1 + rw;
                        y = 11 + (cl / 10 - 1) * 19;
                    } 
                    exAgent.SetCellValue(x, y, scope);
                    src = dwQPSTEP["QPMMEEH"].ToString().TrimEnd();
                    sgb = GetGB2312Value(src);
                    x = xstart + row;
                    y = 5 + ix * 19;
                    if (rw > 0)
                    {
                        x = xstart - 1 + rw;
                        y = 15 + (cl / 10 - 1) * 19;
                    }
                    exAgent.SetCellValue(x, y, src);//单位

                    src = dwQPSTEP["QPMNORM"].ToString().TrimEnd();
                    src = Helper.GetQPTerm(dsSource, src);
                    x = xstart + row;
                    y = 5 + ix * 19;
                    if (rw > 0)
                    {
                        x = xstart - 1 + rw;
                        y = 17 + (cl / 10 - 1) * 19;
                    }
                    exAgent.SetCellValue(x, y, src);//标准
                    ix++;
                    if (ix == 2)
                    {
                        ix = 0;
                        row++;
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        private void ServerShareFile(string KRS, string year, string month, string ordernumer, string fileType)
        {
            try
            {
                string destfile, tempfile;
                ordernumer = "任务书" + ordernumer;
                #region=========一般用户===================
                if (!IsOnAuditing)
                { 
                    #region==========检查临时文件是否已经存在，存在则删除=======================
                    filename = destfolder + "\\" + ordernumer + ".xls"; ;
                    if (File.Exists(filename))
                    {
                        File.Delete(filename);
                    }
                    #endregion
                    #region==========产生文件，复制到服务器=======================
                    destfile = filename;
                    tempfile = CreateFile(ordernumer);
                    if (File.Exists(destfile))
                    {
                        File.Delete(destfile);
                    }
                    File.Move(tempfile, destfile);
                    #endregion
                    #region=========打开服务器上的文件进行浏览========================
                    if (fileType == "OpenExcel")
                    {
                        System.Diagnostics.Process.Start("EXCEL.EXE", filename);
                    }
                    #endregion
                    return;
                }
                #endregion

                #region=========审核用户===================
                #region==========EXCEL/预览==============================
                if (!IsApproved)
                {
                    #region==========检查临时文件是否存在，存在则打开，不存在，则返回=============
                    filename =destfolder+"\\" + ordernumer + ".xls"; ;
                    if (File.Exists(filename))
                    {
                        System.Diagnostics.Process.Start("EXCEL.EXE", filename);
                    }
                    else
                    {
                        MessageBox.Show("亲，跟单员尚未产生指令表。hold住，hold住。");
                    }
                    #endregion
                    return;
                }
                #endregion

                #region==========发布==============================

                #region=========检查临时文件是否存在，存在则移动备份===================
                filename = destfolder + "\\" + ordernumer + ".xls"; ;
                if (File.Exists(filename))
                {
                    destfile = destfolder + "\\" + ordernumer
                                + "_"
                                + DateTime.Now.ToShortDateString().Replace("/", "_")
                                + "_"
                                + DateTime.Now.ToShortTimeString().Replace(":", "_")
                        + "跟单初始版本.xls";
                    File.Move(filename, destfile);
                }
                #endregion

                #region=========检查是否已经审批过，存在则移动备份===================
                filename = destfolder+ "\\" + ordernumer + ".xls"; ;
                if (File.Exists(filename))
                {
                    destfile = Root + @"备份\" + KRS + "\\" + year + "\\" + month + ordernumer + "_"
                                + DateTime.Now.ToShortDateString().Replace("/", "_")
                                + "_"
                                + DateTime.Now.ToShortTimeString().Replace(":", "_")
                                + ".xls";
                    File.Move(filename, destfile);
                }
                #endregion

                #region==========产生文件，复制到服务器=======================
                destfile = Root+ @"\已审批\" +ordernumer + ".xls";
                filename = destfile;
                tempfile = CreateFile(ordernumer);
                File.Move(tempfile, destfile);
                #endregion

                #region=========打开服务器上的文件进行浏览========================
                if (fileType == "OpenExcel")
                {
                    System.Diagnostics.Process.Start("EXCEL.EXE", filename);
                }
                #endregion
                #endregion
                #endregion
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private string CreateFile(string orderno)
        {
            string filename = "";
            try
            {
                string pwd = "";
                sheetName = orderno;
                exAgent.Open(TemplatePath);
                exAgent.Company = strCompany;
                exAgent.strExcelFile = strExcelFile;
                exAgent.CreateNewSheet(orderno + ".xls", Standard, sheetName, pwd);
                filename = exAgent.FileName;
                //exAgent.SetCurrentSheet(2);
                //exAgent.RenameSheet("物测计划");
                exAgent.SetCurrentSheet(1);
                FillInExcelData(columns, 0, 0);
                exAgent.Save();
                exAgent.Close();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return filename;
        }
    }
}
